#include<iostream>
using namespace std;
#include<vector>
#include<stack>
class Solution {
public:
    vector<int> dailyTemperatures(vector<int>& temperatures) {
        stack<int> sta;
        sta.push(0);
        int n = temperatures.size();
        vector<int> ans(n);
        for (int i = 1; i < n; ++i) {
            while (!sta.empty() && temperatures[i] > temperatures[sta.top()]) {
                ans[sta.top()] = i - sta.top();
                sta.pop();
            }
            sta.push(i);
        }
        return ans;
    }
};